@echo off
title select Visual Studio Version
CHOICE /C 012345 /M "Select 1 for 2008, 2 for 2010, 3 for 2012, 4 for 2013, 5 for 2015 or 0 to cancel"

SET SELECTION=%ERRORLEVEL%
rem if exist  "activation.htm" start activation.htm
@echo %SELECTION% 
cls

IF %SELECTION% EQU 1 exit /b 0
IF %SELECTION% EQU 2 (
SET VER=9
if exist  "activation.htm" start activation.htm
cd VS2008
)
IF %SELECTION% EQU 3 (
SET VER=10
if exist  "activation.htm" start activation.htm
cd VS2010
)
IF %SELECTION% EQU 4  (
SET VER=11
if exist  "activation.htm" start activation.htm
cd VS2012
)
IF %SELECTION% EQU 5 (
SET VER=12
if exist  "activation.htm" start activation.htm
cd VS2013
)

IF %SELECTION% EQU 6 (
SET VER=14
if exist  "activationextension.htm" start activationextension.htm
cd VS2015
rem if exist  "activation2.htm" start activation.htm
title uninstalling vsix for Visual Studio %VER%.0...

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"

rem @echo If you have UAC enabled or you are not an administrator
rem @echo You might have to allow the VSIXInstaller.exe to run as admin once or twice 
rem @echo and again when Visual Studio starts the first time
@echo uninstalling old DesignPatterns extension...
title uninstalling old DesignPatterns extension...
START /WAIT VSIXInstaller.exe /q /u:DesignPatterns.Microsoft.9b3cd731-0904-45a1-b3fb-eb9c584a46e8

@echo cleaning registry...
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Premium CSharp\VS2015"  /f 
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Default CSharp\VS2015"  /f
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Premium ManagedCPP\VS2015"  /f
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Default ManagedCPP\VS2015"  /f 
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Premium VB\VS2015"  /f 
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Default VB\VS2015"  /f 
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Premium NativeCPP\VS2015"  /f
reg DELETE "HKCU\Software\DesignPatterns\Plugins\Default NativeCPP\VS2015"  /f  
reg DELETE "HKCU\Software\DesignPatterns\VS2015" /f

@echo installing new DesignPatterns extension...
title installing new DesignPatterns extension...
START /WAIT VSIXInstaller.exe /q designpatterns.vsix
rem @echo starting Microsoft Visual Studio 14.0...
rem "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
rem @echo %path%
@echo done...
pause
exit /b %errorlevel%
)

IF "%SELECTION%" == "9009" (
rem cls
@echo this version of windows does not support choice command
@echo Please navigate manually to the appropriate folder and start setup.exe
pause
exit /b 0
)

rem cls
@echo selection was %SELECTION%
@echo final ver = %VER%
rem pause

title checking for installs
tasklist /v /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "SESSIONNAME eq Console"  /FI "IMAGENAME eq msiexec.exe" | findstr /i msiexec>NUL 
IF %ERRORLEVEL% EQU 0 (
rem cls
@echo some other application is installing, it's recommended to finish it before continuing
pause
)
rem cls
title installing add-in for Visual Studio %VER%.0...
setup.exe

rem only VS2008 setup won't attach to the console
IF %SELECTION% EQU 2 (
:wait
cls
@echo waiting for installation to end...
tasklist /v /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "IMAGENAME eq msiexec.exe" | findstr Console>NUL 
IF %ERRORLEVEL% EQU 0 (
ping -n 3 127.0.0.1 >NUL
goto wait
)
cls
)

rem pause

@echo setup exited with %ERRORLEVEL%, starting Visual Studio %VER%.0 with reset
title reseting add-in for Visual Studio %VER%.0....
IF DEFINED PROGRAMFILES(x86)  (
@echo This is a 64 bit OS
"%PROGRAMFILES(x86)%\Microsoft Visual Studio %VER%.0\Common7\IDE\devenv.exe" /resetaddin DesignPatterns.Connect /Command File.Exit
) else (
@echo This is a 32 bit OS
"%PROGRAMFILES%\Microsoft Visual Studio %VER%.0\Common7\IDE\devenv.exe" /resetaddin DesignPatterns.Connect /Command File.Exit
)
rem pause
cd ..
title show readme.txt if available
rem start /w notepad readme.txt
if exist  "readme.txt" start notepad readme.txt

@echo exiting with %ERRORLEVEL%
exit /b %ERRORLEVEL%

IF DEFINED PROGRAMFILES(x86)  (
@echo This is a 64 bit OS
call "%PROGRAMFILES(x86)%\Microsoft Visual Studio %VER%.0\VC\vcvarsall.bat" x86
) else (
@echo This is a 32 bit OS
call "%PROGRAMFILES%\Microsoft Visual Studio %VER%.0\VC\vcvarsall.bat" x86
)
devenv /resetaddin * /Command File.Exit